package com.itheima.api.array;

public class ArrarSortDemo {

    public static void main(String[] args) {

        int[] arr = {75,69,35,81,27,63,42};
        printArray(arr);
        quickSort(arr, 0, arr.length - 1);
        printArray(arr);
    }

    public static void printArray(int[] arr){
        for(int i : arr){
            System.out.print(i + ",");
        }
        System.out.println();
    }

    public static int partition(int[] arr, int minIndex, int maxIndex){
        int temp = arr[minIndex];
        while (minIndex < maxIndex) {
            while (arr[maxIndex] >= temp && minIndex < maxIndex) {
                maxIndex--;
            }
            if (minIndex < maxIndex) {
                arr[minIndex] = arr[maxIndex];
                minIndex++;
            }
            while (arr[minIndex] < temp && minIndex < maxIndex) {
                minIndex++;
            }
            if (minIndex < maxIndex) {
                arr[maxIndex] = arr[minIndex];
                maxIndex--;
            }
        }
        arr[maxIndex] = temp;
        return maxIndex;
    }

    public static void quickSort(int[] arr, int minIndex, int maxIndex){
//        int partitionIndex = partition(arr, minIndex, maxIndex);
        if (minIndex < maxIndex){
            int partitionIndex = partition(arr, minIndex, maxIndex);
            quickSort(arr, minIndex, partitionIndex - 1);
            quickSort(arr, partitionIndex + 1, maxIndex);
        }
    }

}
